<template>
  <div class="login">
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' },
          {
            pattern: /^[a-z_$][0-9a-z_$]{3,9}$/i,
            message: '只能是数字字母_$组成不能是数字开头长度4到10位',
          },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
          { required: true, message: '请填写密码' },
          {
            pattern: /^[0-9a-z]{4,12}$/i,
            message: '密码数字字母组成长度4到12位',
          },
        ]"
      />

      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { userLogin } from "@/service/mineService";
export default {
  data() {
    return {
      username: "", //用户名
      password: "", //密码
    };
  },
  methods: {
    async onSubmit(values) {
      let res = await userLogin(values);
      //  console.log(res);
      if (res.data.code == 200) {
        window.localStorage.setItem("token", res.data.token);
        this.$router.push("/");
      } else {
        this.$toast.fail(res.data.msg);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  margin-top: 20px;
  padding-left: 2px;
  padding-right: 2px;
}
</style>
